package com.ruoyi.project.system.controller;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TRecruitPersonExamine;
import com.ruoyi.project.system.service.ITRecruitPersonExamineService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 见习记录Controller
 *
 * @author ruoyi
 * @date 2021-09-12
 */
@RestController
@RequestMapping("/person/examine")
public class TRecruitPersonExamineController extends BaseController {
    @Autowired
    private ITRecruitPersonExamineService tRecruitPersonExamineService;

    /**
     * 查询见习记录列表
     */
    @PreAuthorize("@ss.hasPermi('person:examine:list')")
    @GetMapping("/list")
    public TableDataInfo list(TRecruitPersonExamine tRecruitPersonExamine) {
        startPage();
        List<TRecruitPersonExamine> list = tRecruitPersonExamineService.selectTRecruitPersonExamineList(tRecruitPersonExamine);
        return getDataTable(list);
    }

    /**
     * 导出见习记录列表
     */
    @PreAuthorize("@ss.hasPermi('person:examine:export')")
    @Log(title = "见习记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRecruitPersonExamine tRecruitPersonExamine) {
        List<TRecruitPersonExamine> list = tRecruitPersonExamineService.selectTRecruitPersonExamineList(tRecruitPersonExamine);
        ExcelUtil<TRecruitPersonExamine> util = new ExcelUtil<TRecruitPersonExamine>(TRecruitPersonExamine.class);
        return util.exportExcel(list, "见习记录数据");
    }

    /**
     * 获取见习记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('person:examine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {

        return AjaxResult.success(tRecruitPersonExamineService.getDetailById(id));
    }

    /**
     * 新增见习记录
     */
    @PreAuthorize("@ss.hasPermi('person:examine:add')")
    @Log(title = "见习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRecruitPersonExamine tRecruitPersonExamine) {
        List<String> personIdList = tRecruitPersonExamine.getPersonIdList();
        personIdList.forEach(i -> {
            tRecruitPersonExamine.setId(IdUtils.fastSimpleUUID());
            tRecruitPersonExamine.setPersonId(i);
            tRecruitPersonExamine.setCreateTime(new Date());
            tRecruitPersonExamine.setCreateBy(getUsername());
            tRecruitPersonExamineService.save(tRecruitPersonExamine);
        });
        return AjaxResult.success();
    }

    /**
     * 修改见习记录
     */
    @PreAuthorize("@ss.hasPermi('person:examine:edit')")
    @Log(title = "见习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRecruitPersonExamine tRecruitPersonExamine) {
        tRecruitPersonExamine.setUpdateTime(new Date());
        tRecruitPersonExamine.setUpdateBy(getUsername());
        return toAjax(tRecruitPersonExamineService.updateById(tRecruitPersonExamine));
    }

    /**
     * 删除见习记录
     */
    @PreAuthorize("@ss.hasPermi('person:examine:remove')")
    @Log(title = "见习记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tRecruitPersonExamineService.removeByIds(CollectionUtils.arrayToList(ids)));
    }
}
